package cn.virens.web.controller.manage.base.upload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.virens.common.RequestUtil;
import cn.virens.components.page.PageParam;
import cn.virens.components.page.PageResult;
import cn.virens.database.mysql.model.base.BaseUploadFile;
import cn.virens.database.service.base.upload.BaseUploadFileService;
import cn.virens.web.common.BaseTableController;

/**
 * 文件管理 - base_upload_file
 *
 * @author Virens
 * @since  2018-08-25 11:04:10
 */
@Controller
@RequestMapping("/manage/base/upload/file")
public class BaseUploadFileContorller extends BaseTableController<BaseUploadFile, BaseUploadFileService> {

	public BaseUploadFileContorller() {
		super("/manage/base/upload/file", "base:upload:file");
		super.disable(API_CREATE, API_UPDATE);
	}

	@RequestMapping("view.jspx")
	public String viewJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("record", mBaseService.selectOne(RequestUtil.getValueStr(request, "id")));

		return returnUrl("view");
	}

	@Override
	protected PageResult<?> listHandle(HttpServletRequest request, PageParam pageParam) throws Exception {
		pageParam.addParam("mimeType", RequestUtil.getValueStr(request, "mimeType"));
		pageParam.addParam("code", RequestUtil.getValueStr(request, "code"));
		pageParam.addParam("name", RequestUtil.getValueStr(request, "name"));

		return super.listHandle(request, pageParam);
	}

	@Override
	protected Model deleteHandle(HttpServletRequest request, Model model, String id) throws Exception {
		if (mBaseService.delete(id, getRootPath(request)) > 0) {
			return renderSuccess(model, "成功删除");
		} else {
			return renderFail(model, "删除失败");
		}
	}

	private static String getRootPath(HttpServletRequest request) {
		return request.getServletContext().getRealPath("/");
	}
}
